﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Html2Jpg
{
    public partial class Form1 : Form
    {
     public Form1()
   {
            InitializeComponent();
         
        }
     private string ImageUrl= "";//图片地址
     private string ImageName="";//图片名称
     private void butSave_Click(object sender, EventArgs e)
     {
         string HtmlUrl = this.txtLink.Text.Trim();
         if (HtmlUrl == "")
         {
             MessageBox.Show("请输入网址");
             return;
         }
        ImageUrl=txtSavePath.Text??@"c:\Users\hbjohnsan\Desktop";
        
         try
         {
             Uri ri = new Uri(this.txtLink.Text);
             Bitmap bit = ImagePrint.GetHtmlImage(ri, 1200);
             ImageName = DateTime.Now.Ticks.ToString();//时间名称
             ImageUrl += "\\" + ImageName + ".jpg";
             bit.Save(ImageUrl, ImageFormat.Jpeg);
             MessageBox.Show("生产成功");
          }
         
         catch
         {
             MessageBox.Show("网址输入有误！");
             return;
         }
     }
            
         
 
       

      private void btnSavePath_Click(object sender, EventArgs e)
         {
             //获取保存路径
             if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
             {
                 if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
                 {
                     ImageUrl = folderBrowserDialog1.SelectedPath;
                     this.txtSavePath.Text = ImageUrl;
                 }
             }    
         }

         //以下为方法二，防止有的网站不能截图，或是只有一小条
     
    }
}
